package com.ifast.album.service.impl;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.ifast.album.dao.AlbumInfoDao;
import com.ifast.album.domain.AlbumInfo;
import com.ifast.album.service.AlbumInfoService;
import com.ifast.common.utils.Const;
import com.ifast.common.utils.Const.album;
import com.luculent.util.extension.api.PageResult;
import com.luculent.util.extension.api.Result;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;


/**
 * 
 * @author shíQíang㊚
 * @date 2019年4月9日09:56:53
 */
@Transactional
@Service("albumInfoService")
public class AlbumInfoServiceImpl extends ServiceImpl<AlbumInfoDao, AlbumInfo> implements AlbumInfoService{
	
	@Value("${httpPath}")
    private String httpPath;
	/**
	 * 试题分页查询
	 */
	@Override
	public PageResult<?> pageList(Const.album albumType,String userId,String topDeptId,String nian,String yue,String ri,Page<AlbumInfo> page) {
		//返回数据进行拼装 
	    return  data2Page(userId,this.selectPage(page, new EntityWrapper<AlbumInfo>()
	    		.eq("DW_CODE",topDeptId) 
	    		.eq("DELFLAG",0) 
	    		.eq("USER_ID", userId)
	    		.eq("CLASSIFY", albumType.getId())
		    	.eq(StringUtils.isNotBlank(nian),"NIAN",nian).orderBy("NIAN",false)
		    	.eq(StringUtils.isNotBlank(yue),"YUE",yue).orderBy("YUE",false)
		    	.eq(StringUtils.isNotBlank(ri),"RI",ri).orderBy("RI",false).orderBy("CREATE_TIME",false)));
	}
	/**
	 * 将查询的试卷信息 进行拼装
	 * */
	private  PageResult<Map<String,Object>> data2Page(String userId,Page<AlbumInfo> pageAll){
		PageResult<Map<String,Object>> pageRes = new PageResult<>(); 
		if(CollectionUtil.isEmpty(pageAll.getRecords())){ 
			pageRes.setPageNum(1);
			pageRes.setHasNext(false);
			return pageRes;
	    } 
		List<Map<String,Object>> lm = Lists.newArrayList();
		pageAll.getRecords().stream().forEach(version ->{
			//相册封面
//			if(StringUtils.isBlank(version.getCoverUrl()))
//				version.setCoverUrl(httpPath+"/img/album.png");
			Map<String,Object> map = BeanUtil.beanToMap(version);
			map.put("albumId",version.getId());
			lm.add(map);
		});
		pageRes.setListData(lm);
		pageRes.setPageNum(pageAll.getCurrent());
		pageRes.setHasNext(pageAll.hasNext()); 
		return pageRes;
	}
	
	
	@Override
	public Result<?> add(Const.album albumType,String userId,String deptId,String dwCode,String name,int sort){
		if(StringUtils.isBlank(name))
			return Result.fail_msg("缺少必要参数"); 
		AlbumInfo info = createAlbum(albumType,userId,deptId,dwCode,name,sort,Const.albumDoType.type_nomal.getValue());
		Map<String,Object> map = BeanUtil.beanToMap(info);
		map.put("albumId",info.getId());
		return Result.ok(map);
	} 
	
	@Override
	public Result<?> edit(album albumType, String userId, String topDeptId, String albumId, String name, int sort) {
		if(StringUtils.isBlank(name) || StringUtils.isBlank(albumId))
			return Result.fail_msg("缺少必要参数");
		
		AlbumInfo info = this.selectById(albumId);
		if(info == null)
			return Result.fail_msg("该相册不存在");
		
		if(sort > 0)
			info.setSort(sort);
		
		this.updateById(info.setAlbumName(name));
		
		return Result.ok();
	}

	@Override
	public Result<?> remove(album albumType, String userId, String topDeptId, String albumId) {
		if(StringUtils.isBlank(albumId))
			return Result.fail_msg("缺少必要参数");
		
		AlbumInfo info = this.selectById(albumId);
		if(info == null)
			return Result.fail_msg("该相册不存在");
		
		this.updateById(info.setDelflag(1));
		
		return Result.ok();
	}
	/**
	 * pc端新增
	 */
	@Override
	public Result<?> save(album albumType, String userId,String deptId, String dwCode, String albumId, String name, int sort) {
		if(StringUtils.isNotBlank(albumId)){
			AlbumInfo info = this.selectById(albumId);
			if(info == null){
				return Result.fail_msg("相册不存在");
			}
			if(sort > 0)
				info.setSort(sort); 
			return this.updateById(info.setAlbumName(name)) ? Result.ok():Result.fail();
		}else{
			return add(albumType,userId,deptId,dwCode,name,sort);
		} 
	}
	@Override
	public Result<?> deleteBatch(List<String> ids){
		if(CollectionUtil.isEmpty(ids)){
			return Result.ok();
		}
		List<AlbumInfo> infos = this.selectBatchIds(ids);
		if(CollectionUtil.isEmpty(infos)){
			return Result.ok();
		}else{
			infos.stream().forEach(info -> info.setDelflag(1));
			return this.updateBatchById(infos)?Result.ok():Result.fail();
		} 
	}
	@Override
	public Result<?> delete(String id) {
		if(StringUtils.isBlank(id))
			return Result.ok();
		 
		AlbumInfo info = this.selectById(id);
		if(info == null){
			return Result.ok();
		}else{ 
			return this.updateById(info.setDelflag(1))?Result.ok():Result.fail();
		} 
	}
	
	@Override
	public AlbumInfo createAlbum(album albumType, String userId,String deptId, String dwCode, String name, int sort,int doType) {
		if(StringUtils.isBlank(name))
			return null;
		Calendar calendar = Calendar.getInstance();
		AlbumInfo info = AlbumInfo.builder()
			.albumName(name)
			.userId(userId)
			.dwCode(dwCode)
			.deptId(deptId)
			.classify(albumType.getId())
			.classifyType(albumType.getValue())
			.classifyName(albumType.getName())
			.albumSize(0)
			.total(0) 
			.nian(calendar.get(Calendar.YEAR))
			.yue(calendar.get(Calendar.MONTH)+1)
			.ri(calendar.get(Calendar.DAY_OF_MONTH)) 
			.sort(sort)
			.doType(doType)
			.build();
		this.insert(info);
		return info;
	}
}